package assets{	
	import com.ww_and_aloa.components.dropDown.DropDownMenu;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import model.AppModel;
	
	public class Intro extends Sprite{
		// Properties
		private var appModel:AppModel;
		
		private var dropDown:DropDownMenu;
		private var submitCountBtn:SubmitBtn;
		private var count:uint;
		
		private var userNameTxField:TxtField;
		private var userArr:Array = [];
		private var container:Sprite;
		
		// Constructor
		public function Intro(){
			trace("[Intro] constructor made");
			this.appModel = AppModel.getInstance();

			container = new Sprite();
			addChild(container);
			
			var capPlayers:uint = 6;
			var capArr:Array = [];
			
			for(var i:uint=0; i < capPlayers; i++){
				capArr.push(i+1);
			}
			
			dropDown = new DropDownMenu(capArr);
			addChild(dropDown);
			
			dropDown.x = 200;
			
			submitCountBtn = new SubmitBtn();
			addChild(submitCountBtn);
			
			submitCountBtn.addEventListener(MouseEvent.CLICK, countPlayersSubmitHandler);
		}
		
		// Methodes
		private function makeTextfieldPlayers():void{
			var yPos: uint;
			
			for(var i:uint=1; i <= count; i++){
				userNameTxField = new TxtField();
				
				container.addChild(userNameTxField);

				userNameTxField.y = yPos;
				userNameTxField.txtField_txt.text = "Player #" + i;
				
				yPos += userNameTxField.height + 5;
			}
			
			var submitBtn:SubmitBtn = new SubmitBtn();
			addChild(submitBtn);
			
			submitBtn.y = yPos;
			
			submitBtn.addEventListener(MouseEvent.CLICK, submitClickHandler);
		}
		
		// Handlers
		private function countPlayersSubmitHandler(e:Event):void{
			count = uint(dropDown.output);
			
			removeChild(dropDown);
			removeChild(submitCountBtn);
			
			makeTextfieldPlayers();
		}
		
		private function submitClickHandler(e:Event):void{
			for(var i:uint=0; i < container.numChildren; i++){
				var currentUser:TxtField = container.getChildAt(i) as TxtField;
				
				userArr.push(currentUser.txtField_txt.text);
			}
			
			appModel.namesArr = userArr;
			appModel.deleteIntro();
		}
	}
}